package com.lelefly.base.serviceImp;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.FlyHistorySearchBean;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.UploadInfoBean;
import com.lelefly.base.po.LeleFlyHistory;
import com.lelefly.base.po.LeleFlyInfo;
import com.lelefly.base.po.LeleflyHistoryInfo;
import com.lelefly.base.proxy.LeleFlyInfoProxy;
import com.lelefly.constant.Constant;

@Service
public class UploadFlyInfoService extends BaseService{

	@Resource
	private BaseDao baseDao;
	
	public Result insertFlyInfo(List<LeleFlyInfo> flyInfos){
		Result result = new Result<>();
		
		try {
			int state = baseDao.saveObjects(flyInfos);
			if (state == Constant.DB_STATUS_FAIL) {
				result.setMes("数据库保存错误");
				result.setState(Constant.HTTP_STATUS_FAIL);
			}else if (state == Constant.DB_STATUS_SUCCESS) {
				result.setMes("插入成功");
				result.setState(Constant.HTTP_STATUS_OK);
			}
		} catch (Exception e) {
			e.printStackTrace();
			getLogger().error("飞行信息插入异常...");
			result.setMes("数据库插入错误");
			result.setState(Constant.HTTP_STATUS_FAIL);
		}
		
		return result;
	}
	
	/**
	 * 插入飞行数据
	 * @param resultList
	 * @return
	 */
	public Result insertFlyingInfo(List<UploadInfoBean> resultList){
		
		Result result = new Result<>();
		
		// 存入飞行信息表
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH)+1;
		List list = LeleFlyInfoProxy.getLeleFlyInfo(month, resultList);
		LeleflyHistoryInfo historyInfo = LeleFlyInfoProxy.getHistoryInfo(resultList);
		
		try {
			baseDao.saveObjects(list);
			baseDao.saveOrUpdateObject(historyInfo);
			result.setState(Constant.HTTP_STATUS_OK);
			result.setMes("上传成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("上传数据失败");
		}
		
		return result;
		
	}
	
	public Result saveHistoryInfo(LeleFlyHistory historyInfo){
		Result result = new Result<>();
		
		try {
			
			int state = baseDao.saveOrUpdateObject(historyInfo);
			
			if (state == Constant.DB_STATUS_FAIL) {
				result.setMes("数据库保存错误");
				result.setState(Constant.HTTP_STATUS_FAIL);
			}else if (state == Constant.DB_STATUS_SUCCESS) {
				result.setMes("插入成功");
				result.setState(Constant.HTTP_STATUS_OK);
			}
		} catch (Exception e) {
			e.printStackTrace();
			getLogger().error("飞行信息插入异常...");
			result.setMes("数据库插入错误");
			result.setState(Constant.HTTP_STATUS_FAIL);
		}
		return result;
	}
	
	
	public Result searchFlyInfo(FlyHistorySearchBean bean){
		
		Result result = new Result();
		StringBuilder sql = new StringBuilder("");
		
		if (StringUtils.isEmpty(bean.getBeginTime()) && StringUtils.isEmpty(bean.getEndTime())) {
			
		}else {
			if (!StringUtils.isEmpty(bean.getBeginTime())) {
				sql.append("from LeleFlyInfo where flightime > '" + bean.getBeginTime() + "'") ;
			}
			if (!StringUtils.isEmpty(bean.getEndTime())) {
				sql.append("and flightime < '" + bean.getEndTime() + "'");
			}
		}
		if (!StringUtils.isEmpty(bean.getPhoneno())) {
			sql.append(" and latitude <> '0.0' and longitude <> '0.0'");
			sql.append("and user_phone_no = '" + bean.getPhoneno() + "'");
			sql.append(" or mobile_phone_no LIKE '%" + bean.getPhoneno() + "'");
		}
		List<LeleFlyInfo> resultList = baseDao.queryObjetReList(sql.toString());
		result.setResultList(resultList);
		result.state = Constant.HTTP_STATUS_OK;
		return result;
	}
}
